
package com.ph.product.api.controller;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.ph.base.BaseController;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.product.entity.PhProductClassify;
import com.ph.shopping.facade.product.service.IPhProductClassifyService;
import com.ph.shopping.facade.product.service.IProductPropertyService;
import com.ph.shopping.facade.product.service.IProductService;
import com.ph.shopping.facade.product.vo.PhProductClassifyVo;

/**
 * @项目：phshopping-facade-
 *
 * 						@描述：
 *
 * 						@作者： yyg
 *
 * @创建时间：2017年3月14日 下午1:14:28
 *
 * @Copyright by yyg
 */
@Controller
@RequestMapping("/api/productClassify")
public class ProductClassifyController extends BaseController {
	
	@Reference(version="1.0.0")
	private IProductPropertyService  iProductPropertyService;
	
	@Reference(version="1.0.0")
	private IPhProductClassifyService iPhProductClassifyService;
	
	@Reference(version="1.0.0")
	private IProductService iProductService;
	
	
	/***
	 * 联动查询
	 * @param id
	 * @return
	 * @throws Exception
	 *
	 */
	@RequestMapping(value="/selectClass")
	@ResponseBody
	public Result selectChildClass(@RequestParam("id") Long id) throws Exception{
		Result result=new Result();
		PhProductClassifyVo phProductClassifyVo=new PhProductClassifyVo();
		//t通过id查询分类级别数据
		phProductClassifyVo.setParentId(id);
		phProductClassifyVo.setDeleteFlag(1);
		List<PhProductClassifyVo> list=this.iPhProductClassifyService.getProductClassifyVoDetail(phProductClassifyVo);
		result.setData(list);
		return result;
	}
	
	/**
	 * 
	 * @description: 进入新增商品分类页面
	 * @return
	 * @author lichao
	 * @date 2017年3月15日
	 */
	@RequestMapping(value = "/toAddClassify", method = RequestMethod.GET)
	public String toAddProductClassify(Model model){
		//获取所有有效一级分类
		PhProductClassifyVo phProductClassifyVo = new PhProductClassifyVo();
		phProductClassifyVo.setClassifyLevel(1);
		phProductClassifyVo.setDeleteFlag(1);
		try {
			List<PhProductClassifyVo> list = iPhProductClassifyService.getProductClassifyVoList(phProductClassifyVo);
			logger.debug("商品一级分类数据: " + new ObjectMapper().writeValueAsString(list));
			model.addAttribute("phProductClassifyVoList", list);
			return "/product/classify/add";
		} catch (Exception e) {
			logger.error("获取商品一级分类异常, ex={}", e);
			return "";
		}
		
	}
	
	/**
	 * 
	 * @description: 新增商品分类
	 * @param phProductClassify 商品分类对象
	 * @return Result
	 * @author lichao
	 * @date 2017年3月15日
	 */
	@RequestMapping(value = "/addClassify", method = RequestMethod.POST)
	@ResponseBody
	public Result addProductProperty(@ModelAttribute PhProductClassify phProductClassify) {
		logger.info("添加商品分类开始，传入参数" + phProductClassify);
		// 后台数据验证开始
		List<String> errorList = phProductClassify.validateForm();
		if (errorList != null) {
			Gson gson = new Gson();
			logger.info("信息检索表[com.ph.shopping.facade.product.entity.phProductClassify]实体中出错验证错误：错误信息如下"
					+ gson.toJson(errorList));
			return getResult(ResultEnum.ENTITY_CHECK_EXCEPTION);
		}
		try {
			phProductClassify.setCreateTime(new Date());
			phProductClassify.setCreateUser(1L);//-----------------需要获取当前用户
			phProductClassify.setDeleteFlag(1);
			phProductClassify.setStatus(2);//先固定为已启用
			if(null == phProductClassify.getSort()){
				phProductClassify.setSort(1);//默认排序为1
			}
			return iPhProductClassifyService.addProductClassify(phProductClassify);
		} catch (Exception e) {
			logger.error("添加商品分类异常, ex={}", e);
			return getResult(ResultEnum.ADD_PRODUCT_CLASSIFY_EXCEPTION);
		}
		
		
	}
	
	/**
	 * 
	 * @description: 进入修改商品分类页面
	 * @return 
	 * @author lichao
	 * @date 2017年3月15日
	 */
	@RequestMapping(value = "/toUpdateClassify", method = RequestMethod.GET)
	public String toUpdateProductClassify(@ModelAttribute PhProductClassifyVo phProductClassifyVo, Model model){
		try {
			//获取需要修改的商品分类对象
			PhProductClassifyVo pClassify = iPhProductClassifyService.getProductClassifyVo(phProductClassifyVo);
			if(pClassify.getClassifyLevel()>=1){
				PhProductClassifyVo pcL1 = new PhProductClassifyVo();
				pcL1.setClassifyLevel(1);
				pcL1.setDeleteFlag(1);
				List<PhProductClassifyVo> pcL1List = iPhProductClassifyService.getProductClassifyVoList(pcL1);
				for (PhProductClassifyVo pcv : pcL1List) {
					if(pcv.getId().equals(pClassify.getId())){
						pcL1List.remove(pcv);
						break;
					}
				}
				logger.debug("商品一级分类数据: " + new ObjectMapper().writeValueAsString(pcL1List));
				model.addAttribute("pcL1List", pcL1List);
				if(pClassify.getClassifyLevel() == 2 ){
					PhProductClassifyVo pcL2 = new PhProductClassifyVo();
					pcL2.setParentId(pClassify.getParentId());
					pcL2.setDeleteFlag(1);
					List<PhProductClassifyVo> pcL2List = iPhProductClassifyService.getProductClassifyVoList(pcL2);
					for (PhProductClassifyVo pcv : pcL2List) {
						if(pcv.getId().equals(pClassify.getId())){
							pcL2List.remove(pcv);
							break;
						}
					}
					logger.debug("商品二级分类数据: " + new ObjectMapper().writeValueAsString(pcL2List));
					model.addAttribute("pcL2List", pcL2List);
					
				}
				if(pClassify.getClassifyLevel() == 3 ){
					//查出当前的上级对象
					PhProductClassifyVo pClassifyParentFind = new PhProductClassifyVo();
					pClassifyParentFind.setId(pClassify.getParentId());
					PhProductClassifyVo pClassifyParent = iPhProductClassifyService.getProductClassifyVo(pClassifyParentFind);
					model.addAttribute("pClassifyParent", pClassifyParent);
					
					//根据上级对象查出上级的兄弟对象(父id'一致)
					PhProductClassifyVo pcL2 = new PhProductClassifyVo();
					pcL2.setParentId(pClassifyParent.getParentId());
					pcL2.setDeleteFlag(1);
					List<PhProductClassifyVo> pcL2List = iPhProductClassifyService.getProductClassifyVoList(pcL2);
					for (PhProductClassifyVo pcv : pcL2List) {
						if(pcv.getId().equals(pClassify.getId())){
							pcL2List.remove(pcv);
							break;
						}
					}
					logger.debug("商品二级分类数据: " + new ObjectMapper().writeValueAsString(pcL2List));
					model.addAttribute("pcL2List", pcL2List);
					
				}
			}
			
			//验证是否被商品所引用,0默認沒有引用, 1有被引用
			int quote = 0;
			switch (pClassify.getClassifyLevel()) {
			case 1:
					if(iPhProductClassifyService.getExistProductByClassifyLevelOne(pClassify) > 0){
						quote = 1;
					}
				break;
			case 2:
					if(iPhProductClassifyService.getExistProductByClassifyLevelTwo(pClassify) > 0){
						quote = 1;
					}
				break;
			case 3:
				if(iProductService.getProductCountByClassify(pClassify.getId()) > 0){
					quote = 1;
				}
				break;
			}
			model.addAttribute("quote", quote);
			logger.debug("商品分类数据: " + new ObjectMapper().writeValueAsString(pClassify));
			model.addAttribute("phProductClassifyVo", pClassify);
			return "/product/classify/update";
		} catch (Exception e) {
			logger.error("获取商品一级分类异常, ex={}", e);
			return "";
		}
	}
	
	
	/**
	 * 
	 * @description: 修改商品分类
	 * @param phProductClassify 商品分类对象
	 * @return Result
	 * @author lichao
	 * @date 2017年3月15日
	 */
	@RequestMapping(value = "/updateClassify", method = RequestMethod.POST)
	@ResponseBody
	public Result updateProductProperty(@ModelAttribute PhProductClassify phProductClassify) {
		logger.info("修改商品分类开始，传入参数" + phProductClassify);
		// 后台数据验证开始
		List<String> errorList = phProductClassify.validateForm();
		if (errorList != null) {
			Gson gson = new Gson();
			logger.info("信息检索表[com.ph.shopping.facade.product.entity.phProductClassify]实体中出错验证错误：错误信息如下"
					+ gson.toJson(errorList));
			return getResult(ResultEnum.ENTITY_CHECK_EXCEPTION);
		}
		try {
			phProductClassify.setUpdateTime(new Date());
			phProductClassify.setStatus(2);//先固定为已启用
			if(null == phProductClassify.getSort()){
				phProductClassify.setSort(1);//默认排序为1
			}
			return iPhProductClassifyService.updateProductClassify(phProductClassify);
		} catch (Exception e) {
			logger.error("修改商品分类异常, ex={}", e);
			return getResult(ResultEnum.ADD_PRODUCT_CLASSIFY_EXCEPTION);
		}
		
		
	}
	/**
	 * 商品类型列表查询页面跳转 
	 * @author hewl
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value="/listPage")
	public String listPage()throws Exception{
		return "/product/classify/list/productClassifyList";
	}
	
	/**
	 * 查询商品类型列表
	 * @author hewl
	 * @param phProductClassifyVo
	 * @return Result
	 * @throws Exception
	 */
	@RequestMapping(value="/getiPhProductClassifyVoList",method=RequestMethod.GET)
	public @ResponseBody Result getiPhProductClassifyVoList(PhProductClassifyVo phProductClassifyVo) throws Exception{
		Result result =new Result();
		result.setData(iPhProductClassifyService.getProductClassifyVoList(phProductClassifyVo));
		return result;
	}
	
	/**
	 * 查询商品类型分页列表
	 * @author hewl
	 * @param phProductClassifyVo
	 * @return Object
	 * @throws Exception
	 */
	@RequestMapping(value="/getiPhProductClassifyVoPageList",method=RequestMethod.GET)
	public @ResponseBody Object getiPhProductClassifyVoPageList(PageBean pageBean,PhProductClassifyVo phProductClassifyVo) throws Exception{
		phProductClassifyVo.setDeleteFlag(1);
		return iPhProductClassifyService.getProductClassifyVoPageList(pageBean,phProductClassifyVo);
	}
	
	/**
	 * 查询商品主从关系详情（不要调用分页插件）
	 * @author hewl
	 * @param phProductClassifyVo
	 * @return Result
	 * @throws Exception
	 */
	@RequestMapping(value="/getProductClassifyVoDetail",method=RequestMethod.GET)
	public @ResponseBody Result getProductClassifyVoDetail(PhProductClassifyVo phProductClassifyVo) throws Exception{
		Result result =new Result();
		result.setData(iPhProductClassifyService.getProductClassifyVoDetail(phProductClassifyVo));
		return result;
	}
	
	/**
	 * 商品分类单条查询
	 * @author hewl
	 * @param phProductClassifyVo:id
	 * @return Result
	 * @throws Exception
	 */
	@RequestMapping(value="/getProductClassifyVo",method=RequestMethod.GET)
	public @ResponseBody Result getProductClassifyVo(PhProductClassifyVo phProductClassifyVo) throws Exception{
		Result result =new Result();
		result.setData(iPhProductClassifyService.getProductClassifyVoDetail(phProductClassifyVo));
		return result;
	}
	/**
	 * 商品分类子级查询
	 * @author hewl
	 * @param phProductClassifyVo
	 * @return Result
	 * @throws Exception
	 */
	@RequestMapping(value="/getProductClassifyVoAchildList",method=RequestMethod.GET)
	public @ResponseBody Result getProductClassifyVoAchildList(PhProductClassifyVo phProductClassifyVo) throws Exception{
		Result result =new Result();
		try {
			List<PhProductClassifyVo> list = iPhProductClassifyService.getProductClassifyVoAchildList(phProductClassifyVo);
			if(list.size()>0){
				result.setCode("200");
				result.setData(list);
			}else{
				result.setCode("300");
				result.setData(list);
				result.setMessage("查询失败");
			}
		} catch (Exception e) {
			result.setCode("500");
			result.setMessage("请联系管理员");
		}
		
		return result;
	}
	
	/**
	 * 逻辑删除商品类型
	 * @author hewl
	 * @param PhProductClassifyVo:id
	 * @return Result
	 */
	@RequestMapping(value="/deletePhProductClassify",method = RequestMethod.GET)
	public @ResponseBody Result deletePhProductClassify(PhProductClassifyVo phProductClassifyVo) throws Exception{
		return iPhProductClassifyService.deleteLogicPhProductClassify(phProductClassifyVo);
	}
}
